<?php
/**
 * common repository file Created by PhpStorm.
 * User: wumengmeng
 * Date: 2019/08/20
 * Time: 06:07
 */
 
 namespace HiCommon\Repository;

use HiCommon\Model\Users;

class UsersRepository extends BaseRepository
{

    public function base_model(){
        return new \HiCommon\Model\Users();
    }

    /**
     * 分页获取全部数据
     *
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_all( $arr_option = [])
    {
        return Users::commonget($arr_option);
    }


    /**
     * 分页获取全部数据
     *
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_all_to_search( $arr_option = [] , $arr_search = [] )
    {
        return Users::where(
            function ($query)
            use($arr_search) {
                if(!empty($arr_search['key_word'])){
                    $query->where('phone', 'like', '%'.$arr_search['key_word'].'%');
                    $query->orwhere('nickname', 'like', '%'.$arr_search['key_word'].'%');
                }
            })->commonget($arr_option);
    }



    /**
     * 分页获取数据
     *
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_list( array $arr_search ,  $arr_option = [])
    {
        return Users::where(
            function ($query)
            use($arr_search) {
                if(!empty($arr_search['phone'])){
                    $query->where('phone',$arr_search['phone']);
                }
            })->commonpage($arr_option);
    }
    
    /**
     * 通过where分页获取数据
     *
     * @param array $arr_where
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_list_by_where($arr_where,$arr_option = [])
    {
        return Users::where($arr_where)->commonpage($arr_option);

    }

    /**
     * 通过where获取所有数据
     *
     * @param array $arr_where
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_all_by_where($arr_where,$arr_option = [])
    {
        return Users::where($arr_where)->commonget($arr_option);

    }
    


    /**
     * 通过where获取所有数据
     *
     * @param array $arr_where
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_all_by_where_between(array $arr_where = [] , array $arr_option = [])
    {
        return Users::where($arr_where)->where('integral_all' ,'>' , 0)->commonget($arr_option);

    }


    /**
     * 通过where获取所有数据(发放老用户礼包获取指定条件用户专业)
     *
     * @param array $arr_where
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_where_list(  array $arr_array = [] ){

        return Users::where(
            function ($query)
            use($arr_array) {
                $query->where('created_at' , '<=' , $arr_array['time1'] );
                if(!empty($arr_array['time2'])){
                    $query->where('created_at' , '>' , $arr_array['time2'] );
                }
            })->where('status', 1 )->commonget(['field'=>['id','integral_all']]);
    }

    /**
     * 根据条件统计数量
     *
     * @param array $arr_where
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_count_by_where($arr_where)
    {
        return Users::where($arr_where)->count();
    }
    
    /**
     * 根据条件统计总和
     *
     * @param array  $arr_where
     * @param string $s_field
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_sum_by_where($arr_where = [],$s_field = 'mark')
    {
        return Users::where($arr_where)->sum($s_field);
    }

    /**
     * 添加一条数据
     *
     * @param array $arr_input
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function add_one($arr_input)
    {
        return Users::create($arr_input);
    }

    /**
     * 查询一条数据
     *
     * @param int $n_id
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_one(int $n_id, $arr_option = [])
    {
        return Users::commonfind($n_id, $arr_option);
    }

    /**
     * 查询一条数据
     *
     * @param int $n_id
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_one_where($arr_where, $arr_option = [])
    {
        if(empty($arr_option['with'])){
            $arr_option['with'] = [];
        }
        if(empty($arr_option['field'])){
            $arr_option['field'] = ['*'];
        }
        return Users::with($arr_option['with'])->select($arr_option['field'])->where($arr_where)->first();
    }

    /**
     * 查询一条数据
     *
     * @param int $n_id
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_one_by_where($arr_where, $arr_option = [])
    {
        return Users::where($arr_where)->commonfirst($arr_option);

    }


    /**
     * 查询一条数据
     *
     * @param int $n_id
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_one_by_whereIn($arr_where, $arr_option = [])
    {
        return Users::whereIn( 'username', $arr_where)->commonget($arr_option);
    }




    /**
     * 通过用户id获取 small_openid
     *
     * @param int $n_id
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_small_openid_by_uid(int $n_id)
    {
        return Users::select(['id','small_openid'])->find($n_id)->small_openid;
    }

    /**
     * 修改一条数据
     *
     * @param array $arr_input
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function edit_one(array $arr_input)
    {
        return Users::where('id',$arr_input['id'])->update($arr_input);
    }

    /**
     * 删除一条数据
     *
     * @param int $n_id
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function delete_one(int $n_id)
    {
        return Users::where('id', $n_id)
                             ->commondelete();
    }

    /**
     * 修改一条数据状态
     *
     * @param int $n_id
     * @param int $n_state
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function change_one_state(int $n_id,int $n_state)
    {
        return Users::where('id',$n_id)->update(['state'=>$n_state]);

    }
    
    /**
     * 字段自增
     *
     * @param int    $n_id
     * @param string $s_field
     * @param int    $n_step
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function increase_num($n_id,$s_field = 'commend_num',$n_step = 1)
    {
        return Users::where('id',$n_id)->increment($s_field,$n_step);
    }

    /**
     * 字段自减
     *
     * @param int    $n_id
     * @param string $s_field
     * @param int    $n_step
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function decrease_num($n_id,$s_field = 'commend_num',$n_step = 1)
    {
        return Users::where('id',$n_id)->decrement($s_field,$n_step);
    }


}